<!--回弹业务数据字典---回弹仪器设备管理---查看-->
<template>
  <div class="department" id="department">
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left">
          <el-radio-group
            v-model="radio"
            size="small"
            @change="modelType($event)"
          >
            <el-radio-button label="待办"></el-radio-button>
            <el-radio-button label="已办"></el-radio-button>
            <!-- #2b5a9d -->
          </el-radio-group>
        </div>
        <!-- 顶部按钮 -->
        <div class="print">
          <div class="printTable">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @click="yj_searchForm(getList)"
            ></el-button>

            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              v-if="getList.modelType == '3' && yj_edit == true"
              @click="edit('编辑')"
              >编辑</el-button
            >

            <el-button
              type="primary"
              icon="el-icon-circle-check"
              size="small"
              v-if="getList.modelType == '3' && yj_submit == true"
              @click="SubmitText(fId)"
              >提交</el-button
            >
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <tableData
          :height="navigatepageNums > 1 ? 'calc(100% - 50px)' : '100%'"
          :config="table_config"
          :dataList="tableData"
          :loading="loading"
          @selection-change="selectionChange"
          @sort-change="sortChange"
          @click-change="rowChange"
          @ClickOnLine="yj_viewForm"
          :page="getList.page"
          :pageSize="getList.pageSize"
        />
        <pagination
          v-if="navigatepageNums > 1"
          :info="queryCriteriaData"
          :treelength="treelength"
          :total="queryCriteriaData.total"
          :pageNum.sync="getList.page"
          :limit.sync="getList.pageSize"
          @pagination="yj_searchForm(getList)"
        />
      </div>
      <!--配合比通知单编号弹框 查看 -->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          :title="dialogTitle"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible"
          v-loading="loadingEditDialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <div style="overflow-y: auto; margin-bottom: 20px">
              <div class="baseTitle">质量巡查信息</div>
              <div class="publicForm" style="height: 285px">
                <publicForm
                  ref="publicForm0"
                  lableWidth="115px"
                  :formObj="formObjShow"
                  :formData="formData"
                  @selectChange="selectChange"
                  @inputChange="inputChange"
                  @inputFocus="inputFocus"
                  :timestamp="formData.fPatrolfileurl"
                  :fileList="fHandlefileList"
                  @delteFile="delteFile"
                >
                </publicForm>
              </div>

              <div class="baseTitle">巡查信息签收</div>

              <div class="publicForm" style="height: 80px">
                <publicForm
                  ref="publicForm1"
                  lableWidth="115px"
                  :formObj="examineShow"
                  :formData="formData"
                  @selectChange="selectChange"
                  @inputChange="inputChange"
                  @inputFocus="inputFocus"
                >
                </publicForm>
              </div>
              <div class="baseTitle">整改信息回复</div>

              <div class="publicForm" style="height: 120px">
                <publicForm
                  ref="publicForm2"
                  lableWidth="115px"
                  :formObj="rectificationShow"
                  :formData="formData"
                  @selectChange="selectChange"
                  @inputChange="inputChange"
                  @inputFocus="inputFocus"
                  :timestamp="formData.fCompletionfile"
                  :fileList="fCompletionfilelist"
                  @delteFile="delteFile"
                >
                </publicForm>
              </div>
              <div style="height: 50px"></div>
            </div>
          </div>
          <div class="newlyAdded_btn" v-if="btnShow">
            <el-button type="primary" @click="yj_saveForm('保存')"
              >保存</el-button
            >
            <el-button type="warning" @click="dialogEditVisible = false"
              >关闭</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getWorkProcessPatrol2PageLists, //主页面列表接口
  getWorkProcessPatrol2ByFid, //详情
  saveWorkprocessPatrol2Bycorrective, //保存
  commitWorkprocessPatrol2Bycorrective, //提交
} from "@/api/request.js";
import TableData from "@/components/HntManage/el-tables"; //主页面列表
import Pagination from "@/components/public/el-pagination"; //主页面分页
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "../regexDetail.js"; //新增弹框配置项

export default {
  inject: ["reload"],
  name: "patrolRectificationText",
  data() {
    return {
      name: "patrolRectificationText",
      // 分页
      navigatepageNums: null, //是否显示分页
      queryCriteriaData: {},
      // 分页
      yj_view: null, //查看
      yj_add: null, //新增
      yj_edit: null, //编辑
      yj_delete: null, //删除
      yj_submit: null, //提交
      yj_send: null, //下发
      yj_receive: null, //签收
      yj_aduit: null, //审核
      radio: "待办",
      // 列表
      getList: {
        fCompanyid: "", // 左侧选中组织机构id
        fQualitygridclass: "", // 上方查询条件质量网格
        fQualitygridtype: "", // 上方查询条件工程网格
        fUnittype: "", //巡查单位类型（集团公司，分子公司，项目公司，监理单位）
        fStatus: "", //信息状态---编辑中（可编辑，可下发），待签收（可签收），执行中（可整改编辑），已结束
        warningLevel: "", // 预警状态 1：表示超期未结束 2：表示超期已结束
        page: 1, // 当前页码
        pageSize: 20, // 每页显示条数
        userId: "", // 查询待办或已办时传 列表页查询不传
        modelType: "3", // 0-待办 1-已办 传2 表示列表页查询   整改编辑页：待办3   已办4
      },
      row: {
        fStatus: "",
        fCompletionresult: "",
      },
      fId: null, //当前行数据fId
      loading: false, //列表转载
      table_config: formJs.table_config,
      tableData: [], //表格列表数据列表
      // 列表

      // 弹框
      timeYMD: null, //当前日期
      btnShow: true,
      dialogTitle: "",
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      formObjShow: formJs.formObjLook, //质量巡查信息
      examineShow: formJs.examineLook, //巡查信息签收

      rectificationShow: formJs.rectification, //整改回复
      rectification: formJs.rectification, //编辑
      rectificationLook: formJs.rectificationLook, //查看

      Sign: "",
      projectUsersList: [], //项目部列表初始值
      fCompletionfilelist: [], //整改回复文件

      formData: formJs.formData,
      saveData: null,
      fHandlefileList: [], //上传文件
      recordfiles: [], //删除文件
      // 弹框

      userInfo: {}, //登录用户
    };
  },
  components: {
    TableData, //主页面列表
    Pagination, //分页
    publicForm, //表单
    // tree, //树
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    // console.log("userInfo", this.userInfo);
  },
  mounted() {
    this.getList = {
      fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
      fQualitygridclass: "", // 上方查询条件质量网格
      fQualitygridtype: "", // 上方查询条件工程网格
      fUnittype: "", //巡查单位类型（集团公司，分子公司，项目公司，监理单位）
      fStatus: "", //信息状态---编辑中（可编辑，可下发），待签收（可签收），执行中（可整改编辑），已结束
      warningLevel: "", // 预警状态 1：表示超期未结束 2：表示超期已结束
      page: 1, // 当前页码
      pageSize: 20, // 每页显示条数
      userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
      modelType: "3", // 0-待办 1-已办 传2 表示列表页查询   整改编辑页：待办3   已办4
    };
    this.getData();
    this.yj_searchForm(this.getList);
    this.btnRole(this.name);

    this.table_config.operation.cols = [
      {
        prop: "fStatus",
        type: "primary",
        handleRow: this.handleRow,
      },
    ];
    this.table_config.operation["specialClass"] = function (rowData) {
      var color = null;
      if (rowData == "待签收") {
        color = "red !important";
      } else if (rowData == "执行中") {
        color = "orange !important";
      } else if (rowData == "已结束") {
        color = "green !important";
      } else {
        color = "#409eff !important";
      }
      return color;
    };
  },
  methods: {
    btnRole(name) {
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });
        this.yj_edit = btnStr.includes("yj_edit");
        this.yj_submit = btnStr.includes("yj_submit");
      } else {
        this.yj_edit = true;
        this.yj_submit = true;
      }
    },
    modelType(e) {
      this.fId = "";
      if (e == "待办") {
        this.getList.modelType = "3";
        this.getList.fCompanyid = this.userInfo.companyId;
        this.getList.userId = this.userInfo.userId;
      } else {
        this.getList.fCompanyid = this.userInfo.companyId;
        this.getList.modelType = "4";
        this.getList.userId = this.userInfo.userId;
      }
      this.yj_searchForm(this.getList);
    },
    // 获取列表数据
    yj_searchForm(id) {
      this.loading = true;
      // getQualityDutyStationLists(id)
      getWorkProcessPatrol2PageLists(id)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.loading = false;
            that.tableData = res.data.pageInfo.list;
            that.queryCriteriaData = res.data.pageInfo;
            that.navigatepageNums =
              that.queryCriteriaData.navigatepageNums.length;
          } else {
            this.$message.error("列表", res.msg);
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("列表");
          this.loading = false;
        });
    },
    //编辑
    edit(titel) {
      this.recordfiles = [];
      this.fCompletionfilelist = [];
      this.dialogTitle = titel;
      this.Sign = titel;
      this.rectificationShow = this.rectification;

      this.btnShow = true;
      if (titel == "编辑") {
        if (this.fId != "" && this.fId != null) {
          // if (this.row.fStatus == "执行中") {
          this.loadingEditDialog = true; //弹框loading
          this.dialogEditVisible = true;
          this.getConcrete(this.fId);
          // } else {
          //   return this.$message.error("请选择信息状态为‘执行中’的数据");
          // }
        } else {
          return this.$message.error("请先选择需要编辑的数据");
        }
      }
    },
    // 获取详情
    getConcrete(id) {
      getWorkProcessPatrol2ByFid({ fId: id })
        .then((res) => {
          if (res.errcode == 0) {
            this.loadingEditDialog = false; //弹框loading
            console.log("详情", res.data);
            this.formData = res.data.yjQmpMsWorkprocessPatrol;
            this.fHandlefileList = res.data.fPatrolfileurllist;
            this.fCompletionfilelist = res.data.fCompletionfilelist;

            if (this.Sign == "编辑") {
              //整改完成日期
              if (
                this.formData.fCompletiondates == "" ||
                this.formData.fCompletiondates == null
              ) {
                this.formData.fCompletiondates = this.timeYMD; //巡查日期
              }
              //整改资料id
              if (
                this.formData.fCompletionfile == "" ||
                this.formData.fCompletionfile == null
              ) {
                this.formData.fCompletionfile = "XCZG" + Date.now(); //整改资料id
                console.log(this.formData.fCompletionfile);
              }

              //整改负责人
              if (
                this.formData.fCompletionleader == "" ||
                this.formData.fCompletionleader == null ||
                this.formData.fCompletionleaderid == "" ||
                this.formData.fCompletionleaderid == null
              ) {
                this.formData.fCompletionleader = this.userInfo.realName;
                this.formData.fCompletionleaderid = this.userInfo.userId;
              }
            }
          } else {
            console.log("详情接口", res.errcode);
          }
        })
        .catch((err) => {
          console.log(err.msg);
        });
    },
    // 保存
    yj_saveForm() {
      this.fId = "";
      if (this.$refs.publicForm2.submitForm()) {
        if (this.fCompletionfilelist.length != 0) {
          var saveData = {
            fId: this.formData.fId, //当前fid
            fCompletionresult: this.formData.fCompletionresult, //整改结果
            fCompletiondates: this.formData.fCompletiondates, //整改完成日期
            fCompletiondescription: this.formData.fCompletiondescription, //整改内容概述
            fCompletionfile: this.formData.fCompletionfile, //整改资料
            fCompletionleaderid: this.formData.fCompletionleaderid, //整改负责人id
            fCompletionleader: this.formData.fCompletionleader, //整改负责人姓名
            recordfiles: this.recordfiles, //删除文件集合
          };
          saveWorkprocessPatrol2Bycorrective(saveData)
            .then((res) => {
              if (res.errcode == 0) {
                this.yj_searchForm(this.getList);
                this.dialogEditVisible = false;
                this.$message.success("保存成功");
              } else {
                this.$message.error(res.msg);
                console.log("接口报错");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          this.$message.warning("请上传整改资料");
        }
      } else {
        console.log("error");
      }
    },
    // 刪除文件
    delteFile(file) {
      for (let index = 0; index < this.fCompletionfilelist.length; index++) {
        if (file.uid == this.fCompletionfilelist[index].uid) {
          if (this.fCompletionfilelist[index].atta == undefined) {
            this.recordfiles.push({
              fId: this.fCompletionfilelist[index].fId,
            });
          } else {
            this.recordfiles.push({
              fId: this.fCompletionfilelist[index].atta.fId,
            });
          }
          this.fCompletionfilelist.splice(index, 1);
        }
      }
    },

    // 提交
    SubmitText(id) {
      console.log(this.row.fStatus, this.row.fCompletionresult, id);
      if (id != "" && id != null) {
        if (
          this.row.fStatus == "执行中" &&
          this.row.fCompletionresult == "已整改"
        ) {
          this.$confirm("是否继续提交?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.loading = true;
              commitWorkprocessPatrol2Bycorrective({
                fId: id,
                userId: this.userInfo.userId,
                userName: this.userInfo.realName,
              })
                .then((res) => {
                  if (res.errcode == 0) {
                    this.loading = false;
                    this.yj_searchForm(this.getList);
                    this.$message.success("提交成功");
                  } else {
                    this.$message.error("提交失败", res.msg);
                    this.loading = false;
                  }
                })
                .catch(() => {
                  console.log("提交失败");
                  this.loading = false;
                });
            })
            .catch(() => {
              this.$message.info("取消提交");
            });
        } else {
          return this.$message.warning(
            "请选择状态为'执行中',整改结果为'已整改'的数据"
          );
        }
      } else {
        return this.$message.error("请先选择需要提交的数据");
      }
    },

    // 点击查看
    yj_viewForm(row, label) {
      this.fId = row.fId;
      if (label == "巡查单位") {
        this.editText(this.fId);
      }
    },

    // 查看
    editText(id) {
      this.rectificationShow = this.rectificationLook;
      this.dialogTitle = "查看";
      this.Sign = "查看";
      this.btnShow = false;
      if (id != "" && id != null) {
        this.loadingEditDialog = true; //弹框loading
        this.dialogEditVisible = true;
        // getQualityDutyStationByFid({ fId: id })
        getWorkProcessPatrol2ByFid({ fId: id })
          .then((res) => {
            if (res.errcode == 0) {
              this.loadingEditDialog = false; //弹框loading
              this.formData = res.data.yjQmpMsWorkprocessPatrol;
              this.fHandlefileList = res.data.fPatrolfileurllist;
              this.fCompletionfilelist = res.data.fCompletionfilelist;
            } else {
              this.$message.error("查看", res.msg);
              this.loadingEditDialog = false;
            }
          })
          .catch(() => {
            console.log("查看---详情接口报错");
            this.loadingEditDialog = false;
          });
      } else {
        return this.$message.error("请先选择需要查看的数据");
      }
    },

    selectChange(value, index, prop) {
      console.log("下拉变", value, index, prop);
    },

    inputChange(value, index, prop) {
      console.log("input变", value, index, prop);
      return value, index, prop;
    },
    inputFocus(index, prop) {
      this.propText = prop;
      console.log("input获焦", index, prop);
    },

    getData() {
      var time = new Date();
      const y = time.getFullYear();
      const m = (time.getMonth() + 1 + "").padStart(2, "0");
      const d = (time.getDate() + "").padStart(2, "0");
      this.timeYMD = `${y}-${m}-${d}`;
    },
    // 点击当前
    rowChange(val) {
      this.row = val;
      this.fId = val.fId;
    },

    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped lang="less">
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;
  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}
/* 弹框 */

.mouldForm /deep/ .el-col-12 .el-form-item:first-child .el-input__suffix {
  left: 84% !important;
}
.publicForm .mouldForm /deep/ .el-form-item__content {
  margin-left: 120px !important;
}
</style>